package com.it.park.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GrapArticle {

	public static  List<String[]> getArticleFrom51CTO(String url){
		url = "http://zhuanlan.51cto.com/";
		List<String[]> list = new ArrayList<String[]>();
		Document doc;
		try {
			doc = Jsoup.connect(url)
					.timeout(5000)
					.get();
			Elements es = doc.select(".piclist>li>a");
			Elements titles = doc.select(".piclist>li h3>a");
			for (int i = 0; i < es.size(); i++) {
				String[] oneArticle = new String[3];
					String href = es.get(i).attr("href");
					String content = getOne(href);
					oneArticle[0] = content;
					Element img = es.get(i).select("img").first();
					oneArticle[1] = img.attr("src");
					oneArticle[2] = titles.get(i).text();
					list.add(oneArticle);
			}
			return list;
		}catch(IOException e){
			e.printStackTrace();
		}
		return null;
	}
	
	public  static String  getOne(String url){
		try {
			Document doc = Jsoup.connect(url)
					.timeout(5000)
					.get();
			int total = doc.select(".main_left .zwnr p").size()-1;
			return doc.select(".main_left .zwnr p:lt("+total+")").toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	public static void main(String[] args) {
		List<String[]> list = getArticleFrom51CTO("http://zhuanlan.51cto.com/");
		for (String[] str : list) {
			System.out.println(str[2]);
		}
	}
}
